#!/bin/sh
. "/usr/local/scripts/build_scripts"/util.sh
source /etc/profile
echo cur_path=`pwd`
cur_dir=`pwd`
echo build dir is $cur_dir and branch=$1,push_action=$2 and channel=$3 and moudle=$4

cd $cur_dir
git checkout $1
git pull
#shell just can return num,not string
if [ "$1" == "master" -o "$1" == "develop" -o "$1" == "newmaster" -o "$1" == "leboapk_develop" ]; then 
    echo  this is master or develop branch and just need build
else
    check_branch $1
    lastest_tag=`cat zbuild_tmp`
    echo lastest_tag=$lastest_tag
    if [ "x$lastest_tag" == "x" ];then
        echo lastest_tag is null and need to create one
        newest_tag="$origin_tag".1
    else
        lastest_tag_num=`echo $lastest_tag |awk -F $origin_tag '{print $2}' |awk -F "." '{print $2}' `
        newest_tag_num=`expr $lastest_tag_num + 1`
        echo newest_tag_num=$newest_tag_num
        newest_tag=$origin_tag."$newest_tag_num"
    fi

    echo newest_tag=$newest_tag
    if [ "$2" == "1" ]; then
        echo will push tag to remote
        git tag $newest_tag
        git push origin $newest_tag
        echo delete local tag
        git tag -d $newest_tag
    else
        echo just need build Package
    fi
fi

echo pull new tag and code again ....
git checkout $1
git pull
if [ "$1" == "master" -o "$1" == "develop" -o "$1" == "newmaster" -o "$1" == "leboapk_develop"  ]; then 
    echo  this is master or develop branch
    now_time=`date "+%Y%m%d%H%M"`
    #ftp_dir="$now_time"_"$1"
    ftp_dir="$1"
    if [ "$4" == "ios"  ];then
        echo have no need get versionName
    else
        if [ -f released_tag_name ];then
            old_tag=`cat released_tag_name`
            if [ "x$old_tag" == "x" ]; then
                echo user have not input released tag
            else
                git checkout $old_tag
                echo lastest commit as follow:
                git log -1
            fi
        else
            echo donot have released_tag_name file
        fi

        echo get versionName
        [ "$4" == "rec_sdk" -o "$4" == "send_sdk" ] &&  cat sdk/build.gradle |grep "versionName \"" && ver_name_tmp=`cat sdk/build.gradle |grep "versionName \""`
        [ "$4" == "send_app" ] && cat app/build.gradle |grep "versionName \"" && ver_name_tmp=`cat app/build.gradle |grep "versionName \""`
        [ "$4" == "rec_app"  ] && cat app/build.gradle |grep "versionName =" && ver_name_tmp=`cat app/build.gradle |grep "versionName ="`
        ver_name=`echo $ver_name_tmp |awk -F "\"" '{print $2}' |sed 's/\.//g'`
        ftp_dir="$ftp_dir"_"$ver_name"
    fi
else
    if [ "$2" == "1" ];then
        dir_tag=$newest_tag
    else
        dir_tag=$lastest_tag
    fi
    echo dir_tag=$dir_tag
    if [ "x$dir_tag" = "x" ];then
        ftp_dir=`echo $1 |sed 's/\.//g'|sed s/^.//g`
    else
        ftp_dir=`echo $dir_tag |awk -F tag_t '{print $2}' |sed 's/\.//g' `
    fi
fi

commit=`git rev-parse --short HEAD`
ftp_dir="$ftp_dir"_"$commit"_"$3"
echo ftp_dir=$ftp_dir
echo build channel is $3

[ "$4" != "ios" ] && pre_build
[ "$4" == "rec_sdk" ] && result_dir="android_receiver_sdk"
[ "$4" == "rec_app" ] && result_dir="android_receiver_app"
[ "$4" == "send_sdk" ] && result_dir="android_sender_sdk"
[ "$4" == "send_app" ] && result_dir="android_sender_app"
[ "$4" != "ios" ] && sh "/usr/local/scripts/build_scripts"/test_android.sh $1 $3 $4
if [ "$4" == "ios" ];then
    git checkout $1
    git pull
    ios_build $3 $ftp_dir
    if [ "$3" == "HPPlayTVAssistant" ];then
        result_dir="ios_send_app"
    else
        result_dir="ios_send_sdk"
    fi
fi
if [ "$4" != "rec_app" -a "$4" != "send_app" -a "$4" != "rec_sdk" -a "$4" == "send_sdk" -a "$4" != "ios" ] ; then 
    echo please input string, rec_sdk or rec_app or send_sdk or send_app or ios ?
    exit 1
fi
upload_file  $result_dir $ftp_dir
